<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Code Embedding</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            max-width: 500px;
            width: 100%;
            padding: 20px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        input[type="file"] {
            display: block;
            margin: 10px auto;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            margin-top: 10px;
            padding: 10px 15px;
            border: none;
            background-color: #007BFF;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #fafafa;
            white-space: pre-wrap;
            word-wrap: break-word;
            text-align: left;
            border-radius: 5px;
            max-height: 300px;  /* Set a maximum height for the result box */
            overflow-y: auto;  /* Add vertical scrolling */
        }
    </style>
</head>
<body>
<div class="container">
    <h2>Upload Code File for Embedding</h2>
    <input type="file" id="codeFile" accept=".txt">
    <button onclick="uploadFile()">Upload and Process</button>
    <div id="result"></div>
</div>

<script>
    async function uploadFile() {
        const fileInput = document.getElementById('codeFile');
        const file = fileInput.files[0];

        if (!file) {
            alert('Please select a file first.');
            return;
        }

        const formData = new FormData();
        formData.append('codeFile', file);

        try {
            const response = await fetch('/process-code', {
                method: 'POST',
                body: formData
            });

            const resultContainer = document.getElementById('result');
            if (response.ok) {
                const result = await response.text();
                resultContainer.innerText = result;
            } else {
                resultContainer.innerText = 'Error processing the file.';
            }
        } catch (error) {
            console.error('Error:', error);
            document.getElementById('result').innerText = 'An error occurred while uploading the file.';
        }
    }
</script>
</body>
</html>
